package com.yylending.las.lending.quota.face;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.StatIDSet;
import com.yylending.las.common.AESEntity;
import com.yylending.las.Constants;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

/**
 * Created by laixj on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class FacePresenter extends Presenter<FaceModel, FacePresenter.View> {
	private final static String TAG = "FacePresenter";

	public interface View extends IView {
		/**
		 * 保存人脸信息
		 */
		void saveFaceSuccess();

		void saveFaceFail(String msg, boolean exit, String retCode);
	}

	public static class Generator implements PresenterFactory<FacePresenter> {
		@Override
		public FacePresenter create() {
			return new FacePresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new FaceModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void saveFaceResult(FaceModel.SaveFaceResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				CLog.i(TAG, "保存人脸信息成功");
				mView.saveFaceSuccess();
			} else {
				if(Constants.CODE_700025.equals(result.getRetCode())){
					mView.saveFaceFail(Constants.MSG_700025, true, result.getRetCode());
				}else{
					mView.saveFaceFail(Constants.MSG_FACE_FAILED, false, result.getRetCode());
				}
			}
		}
	}

	/**
	 * 保存人脸信息
	 */
	public void saveFaceInfo(String sface) {
		CLog.i(TAG, "刷脸信息-->" + sface);
		execute(new AESEntity(new FaceModel.SaveFace(sface)), FaceModel.METHOD_SAVE, true, null, mRequestPage);
	}
}